var fs = require("fs");
var path = require("path");

var export_files = ["utils", "constant", "point_map", "ad_util", "ocrUtils"];
var generate_files = [
  "ad",
  "bless",
  "gold_video",
  "read_ad",
  "read",
  "treasure",
  "video",
  "novel",
];

var all_folder = "百度极速版_all";
var run_folder = "百度极速版_run";
var module_folder = "百度极速版_module";

//run: export_code
var export_code = export_files
  .map(function (file) {
    var code = fs.readFileSync(
      path.join(__dirname, `../${all_folder}/exports/${file}.js`),
      {
        encoding: "utf-8",
      }
    );
    return code.replace(/var[\w\W]+?require[\w\W]+?;/gi, "");
  })
  .join("\n");

generate_files.forEach(function (file) {
  var code = fs.readFileSync(
    path.join(__dirname, `../${all_folder}/${file}.js`),
    {
      encoding: "utf-8",
    }
  );
  var final_code =
    export_code +
    "\n" +
    code.replace(/var[\w\W]+?require[\w\W]+?;/gi, "") +
    "\n" +
    file +
    ".start()";
  fs.writeFileSync(
    path.join(__dirname, `../${run_folder}/${file}.js`),
    final_code,
    {
      encoding: "utf-8",
    }
  );
});

//module: 生成module文件
export_files.forEach(function (file) {
  var code = fs.readFileSync(
    path.join(__dirname, `../${all_folder}/exports/${file}.js`),
    {
      encoding: "utf-8",
    }
  );
  var module_code = code + "\n" + "module.exports=" + file;
  fs.writeFileSync(
    path.join(__dirname, `../${module_folder}/exports/${file}.js`),
    module_code,
    {
      encoding: "utf-8",
    }
  );
});

//module: require_code
var require_code = export_files
  .map(function (export_file) {
    return "var " + export_file + " = require('./exports/" + export_file + "')";
  })
  .join("\n");

//module: 生成文件
generate_files.forEach(function (file) {
  var code = fs.readFileSync(
    path.join(__dirname, `../${all_folder}/${file}.js`),
    {
      encoding: "utf-8",
    }
  );
  var final_code =
    require_code + "\n" + code + "\n" + "module.exports = " + file;
  var run_code = require_code + "\n" + code + "\n" + file + ".start()";
  fs.writeFileSync(
    path.join(__dirname, `../${module_folder}/${file}_run.js`),
    run_code,
    {
      encoding: "utf-8",
    }
  );
  fs.writeFileSync(
    path.join(__dirname, `../${module_folder}/${file}.js`),
    final_code,
    {
      encoding: "utf-8",
    }
  );
});

//module: require_code
var require_code_action = generate_files
  .map(function (generate_file) {
    return "var " + generate_file + " = require('./" + generate_file + "')";
  })
  .join("\n");

var main_code = `function main() {
  while (true) {
    sleep(2 * 1000);
    bless.go_page_from_home();
    sleep(2 * 1000);
    video.go_page_from_home();
    sleep(2 * 1000);
    gold_video.go_page_from_home();
    sleep(2 * 1000);
    treasure.go_page_from_home();
    if (utils.exists_text_starts_with("明天再来")) {
      return;
    }
    sleep(2 * 1000);
    ad.go_page_from_home();
    sleep(2 * 1000);
    console.show();
    console.log("在等待下一个轮回,5s");
    sleep(5 * 1000);
    console.hide();
    sleep(2 * 1000);
  }
}
main();`;

//module: 生成main
fs.writeFileSync(
  path.join(__dirname, `../${module_folder}/main.js`),
  require_code + "\n" + require_code_action + "\n" + main_code,
  {
    encoding: "utf-8",
  }
);
//run: 生成main

//run: export_code
var export_code_action = generate_files
  .map(function (generate_file) {
    var code = fs.readFileSync(
      path.join(__dirname, `../${all_folder}/${generate_file}.js`),
      {
        encoding: "utf-8",
      }
    );
    return code.replace(/var[\w\W]+?require[\w\W]+?;/gi, "");
  })
  .join("\n");

fs.writeFileSync(
  path.join(__dirname, `../${run_folder}/main.js`),
  export_code + "\n" + export_code_action + "\n" + main_code,
  {
    encoding: "utf-8",
  }
);
